package com.jcfk.system.controller;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.controller.BaseController;
import com.jcfk.common.business.enums.AvailableStatus;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.business.utils.DataBindHelper;
import com.jcfk.common.core.utils.ExcelUtils;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.system.domain.dto.SysRoleDTO;
import com.jcfk.system.domain.excel.SysRoleExcel;
import com.jcfk.system.domain.query.SysRoleQuery;
import com.jcfk.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;


/**
 * 组织机构信息维护 服务接口
 *
 * @author awei
 */
@Api(value = "/sysRole", tags = {"角色管理"})
@RestController
@RequestMapping("/sysRole")
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService sysRoleService;


    /**
     * 查询角色信息列表(分页)
     *
     * @param requestParam
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<SysRoleQuery>", name = "requestParam", value = "", required = true)})
    @ApiOperation(value = "查询角色信息列表(分页)", notes = "查询角色信息列表(分页)", httpMethod = "POST")
    @PostMapping("/queryByPage")
    public RetPage<SysRoleDTO> queryByPage(@RequestBody ReqPageParam<SysRoleQuery> requestParam) {

        DataPage<SysRoleDTO> page = new DataPage<>((long) requestParam.getCurrent(), (long) requestParam.getSize(), requestParam.getSort().getOrderItem(), SysRoleDTO.class);

        return new RetPage<>(sysRoleService.queryByPage(page, requestParam.getData()));
    }

    /**
     * 添加/更新管理角色
     *
     * @param dto
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "SysRoleDTO", name = "dto", value = "", required = true)})
    @ApiOperation(value = "添加/更新管理角色", notes = "添加/更新管理角色", httpMethod = "POST")
    @RequestLogHeader(modular = "角色管理", businessType = "保存")
    @PostMapping("/save")
    @ResponseBody
    public RetResult<?> save(@NonNull @RequestBody @Valid SysRoleDTO dto) {
        // 验证角色名是否重复
        RetResult ret = sysRoleService.isValid(dto);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }
        if (StrUtil.isEmpty(dto.getId())) {
            return RetResult.ok(sysRoleService.insert(dto));
        } else {
            return RetResult.ok(sysRoleService.update(dto));
        }
    }


    /**
     * 逻辑删除角色
     *
     * @param dto
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "SysRoleDTO", name = "sysUser", value = "", required = true)})
    @ApiOperation(value = "逻辑删除角色", notes = "逻辑删除角色", httpMethod = "POST")
    @RequestLogHeader(modular = "角色管理", businessType = "删除")
    @PostMapping("/delete")
    @ResponseBody
    public RetResult<?> delete(@NonNull @RequestBody SysRoleDTO dto) {
        return RetResult.ok(sysRoleService.logicDeleteList(dto));
    }


    /**
     * 角色管理
     *
     * @param id
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "path", dataType = "string", name = "id", value = "", required = true)})
    @ApiOperation(value = "角色管理", notes = "角色管理", httpMethod = "GET")
    @GetMapping("/get/{id}")
    @ResponseBody
    public RetResult<SysRoleDTO> get(@NonNull @PathVariable("id") String id) {
        SysRoleDTO sysUser = sysRoleService.get(id);
        return RetResult.ok(sysUser);
    }

    /**
     * 查询角色信息列表
     *
     * @param sysRoleQuery
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "SysRoleQuery", name = "sysRoleQuery", value = "", required = true)})
    @ApiOperation(value = "查询角色信息列表", notes = "查询角色信息列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<?> getList(@RequestBody SysRoleQuery sysRoleQuery) {
        List<SysRoleDTO> list = sysRoleService.getList(sysRoleQuery);
        // 绑定启用标志文本
        DataBindHelper.bindDataLabel(list, AvailableStatus.getMap(), "enabled", "enabledLabel");
        return RetResult.ok(list);
    }


    /**
     * 角色管理 查询列表 用于角色分配
     *
     * @return
     */
    @ApiOperation(value = "角色管理 查询列表 用于角色分配", notes = "角色管理 查询列表 用于角色分配", httpMethod = "POST")
    @PostMapping("listALL")
    public RetResult<List<SysRoleDTO>> listAll() {
        return RetResult.ok(sysRoleService.listAll());
    }


    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody ReqPageParam<SysRoleQuery> requestParam, HttpServletResponse response) throws Exception {
        List<SysRoleDTO> list = sysRoleService.queryByPage(requestParam.getData());
        ExcelUtils.exportExcelToTarget(response, null, list, SysRoleExcel.class);
    }
}
